package com.yztc.ui.category;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.yztc.config.Url;
import com.yztc.helper.DividerItemDecoration;
import com.yztc.helper.OkHttpClientHelper;
import com.yztc.okbuy.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by My on 2016/12/19.
 */

public class DetailFragment extends Fragment{
    private Context mContext=null;
    private RecyclerView recyclerView_icon;
    private List<CartgroyBean.ResultsBean> mList=new ArrayList<>();
    private RecyclerIconAdapter adapter;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    List<CartgroyBean.ResultsBean> mList= (List<CartgroyBean.ResultsBean>) msg.obj;

                    GridLayoutManager manager=new GridLayoutManager(mContext,4);
                    recyclerView_icon.setLayoutManager(manager);
                    adapter=new RecyclerIconAdapter(mContext,mList);
                    recyclerView_icon.setAdapter(adapter);
                    break;
            }
        }
    };
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_detail,null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView_icon= (RecyclerView) view.findViewById(R.id.recyclerView_icon);
//        recyclerView_icon.setHasFixedSize(true);
//        recyclerView_icon.addItemDecoration(new DividerItemDecoration(mContext,1));
//        LinearLayoutManager manager=new LinearLayoutManager(mContext);
//        recyclerView_icon.setLayoutManager(manager);
//        Log.i("tag","!!!!!******#####"+mList);
//        adapter=new RecyclerIconAdapter(mContext,mList);
//        recyclerView_icon.setAdapter(adapter);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String jsonString= OkHttpClientHelper.getStringFromURL(mContext, Url.CLASSIFY,1);
//                    Log.i("tag","!!!!!!!!!!!1"+jsonString);
                    Gson gson=new Gson();
                    CartgroyBean bean=gson.fromJson(jsonString,CartgroyBean.class);
                    List<CartgroyBean.ResultsBean> list=bean.getResults();
//                    Log.i("tag","!!!!!******"+list);

                    mList=bean.getResults();
                    Message msg=new Message();
                    msg.what=0;
                    msg.obj=list;
                    handler.sendMessage(msg);
                } catch (IOException e) {
                }
            }
        }).start();
    }
}
